require('assets/css/common.css')

import Vue from 'vue'

import _ajax from './ajax'
import { settings, configs, redirects } from './localStorage'

import vueFilter from './vueFilter'

//解决click点击300毫秒延时问题
// import FastClick from 'fastclick';
// FastClick.attach(document.body);

let setting = settings.getSettings()

setting.webapiDomain = 'http://gzfhxxkj.com'
// setting.webapiDomain = 'http://192.168.33.163:80'
// setting.webapiDomain = 'http://localhost:8091'
settings.setSettings(setting);

window.toPage = (options) => {
  if (!options) throw Error('toPage must have params options')
  mui.openWindow({
    "id": options.id,
    "url": options.url,
    show: {
      autoShow: true
    },
    waiting: {
      title: '加载中...'
    }
  })
}

export const openPage = function( url ) {
    mui.openWindow({
        id: url || "building",
        url: url || "building.html",
        show: { autoShow: true },
        waiting: { title: '加载中...' }
    });
};

export const locationParams = function( params ) {
    var obj = {}, i, parts, len, key, value;
    if ( typeof params === 'string' ) {
        value = location.search.match( new RegExp( '[?&]' + params + '=?([^&]*)[&#$]?' ));
        return value ? decodeURIComponent( value[ 1 ]) : undefined;
    }
    var _params = location.search.substr( 1 ).split( '&' );
    for ( i = 0, len = _params.length; i < len; i++ ) {
        parts = _params[ i ].split( '=' );
        if ( !parts[0] ) continue;
        obj[ parts[ 0 ]] = parts[ 1 ] && decodeURIComponent( parts[ 1 ]) || true;
    }
    if ( typeof params !== 'object' ) return obj;
    for ( key in params ) {
        value = params[ key ];
        if ( typeof value === 'undefined' ) delete obj[ key ];
        else obj[ key ] = value;
    }
    parts = [];
    for ( key in obj ) parts.push( key + ( obj[ key ] === true ? '' : '=' + obj[ key ]));
    location.search = parts.join( '&' );
}

export const callWechatPay = function (params, onSuccess) {
    const callback = () => {
        // alert(JSON.stringify(params));
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            params,
            function (res) {
                WeixinJSBridge.log(res.err_msg);

                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    alert("支付成功");
                    if (onSuccess) {
                        onSuccess();
                    }
                }else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                    alert("支付取消");
                }else{
                    alert(res.err_code + res.err_desc + res.err_msg);
                }
            }
        );
    }

    if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', callback, false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', callback);
            document.attachEvent('onWeixinJSBridgeReady', callback);
        }
    } else {
        callback();
    }
}

export const ajax = _ajax
export const userSettings = settings
export const userConfig = configs
export const userRedirect = redirects
export const apiDomain = 'http://resources.gzfhxxkj.com'
// export const apiDomain = setting.webapiDomain
export const fetchDomain = 'http://gzfhxxkj.com'
